package com.why.serviceorder.service.impl;

import com.why.commonresults.CourseOrder;
import com.why.commonresults.UcenterMemberOrder;
import com.why.serviceorder.bean.TOrder;
import com.why.serviceorder.client.EduClilent;
import com.why.serviceorder.client.UserClient;
import com.why.serviceorder.mapper.TOrderMapper;
import com.why.serviceorder.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.why.serviceorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author why
 * @since 2021-02-22
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private EduClilent eduClilent;

    @Autowired
    private UserClient userClient;

    /**
     * 先获取用户的信息，课程的信息最后生成订单
     *
     * @param userId
     * @param courseId
     * @return
     */
    @Override
    public String createOrderByCourseId(String userId, String courseId) {
        //远程调用获取用户信息
        UcenterMemberOrder ucenterMember = userClient.getInfoById(userId);
        //远程调用获取课程信息
        CourseOrder courseDto = eduClilent.getCourseInfoDto(courseId);


        //创建订单对象
        TOrder order = new TOrder();
        //订单号
        order.setOrderNo(OrderNoUtil.getOrderNo());
        //课程id
        order.setCourseId(courseId);
        //课程名字
        order.setCourseTitle(courseDto.getTitle());
        //课程头像
        order.setCourseCover(courseDto.getCover());
        //老师
        order.setTeacherName(courseDto.getTeacherName());
        //价格
        order.setTotalFee(courseDto.getPrice());
        order.setMemberId(userId);
        order.setMobile(ucenterMember.getMobile());
        order.setNickname(ucenterMember.getNickname());
        order.setStatus(0);//支付状态，只是生成订单，还未支付
        order.setPayType(1);//支付类型
        baseMapper.insert(order);

        //生成订单，返回订单号
        return order.getOrderNo();
    }

}
